from azure.storage.blob._container_client import ContainerClient
from azure.storage.blob._blob_client import BlobClient
from azure.storage.blob._blob_service_client import BlobServiceClient
from azure.storage.blob._container_client import ContainerClient

connection_string = "DefaultEndpointsProtocol=https;AccountName=dataaggregate001;AccountKey=/pnKVSJdy/Ri0N5LXgdKqRvfqp4Qw5ITubK4MdaGGlE+pruX5iQJBqjVhMECkzBO0hTV5Y47wRDgNxU5TU8W0A==;EndpointSuffix=core.windows.net"
container = ContainerClient.from_connection_string(conn_str=connection_string, container_name="test")

def upload_content(content, path):
    for i in range(3):
        try:
            container.upload_blob(data=content, name=path)
            break
        except:
            continue

# lit = container_content_list(container,"test/linnean_MMS")


def container_content_list(connection_instance, blob_path):
    try:
        blob_name_list = []
        source_blob_list = connection_instance.list_blobs(name_starts_with=blob_path)
        print(source_blob_list)
        for blob in source_blob_list:
            blob_name = blob.name.rsplit('/', 1)[1]
            blob_name_list.append(blob_name)
            print(blob_name)
    except Exception as ex:
        print("Error: " + str(ex))
#
def azure_connect_conn_string(source_container_connection_string, source_container_name):
    try:
        blob_source_service_client = BlobServiceClient.from_connection_string(source_container_connection_string)
        source_container_client = blob_source_service_client.get_container_client(source_container_name)
        print("Connection String -- Connected.")
        return source_container_client
    except Exception as ex:
        print("Error: " + str(ex))


def main():
    try:
        azure_connection_string = "DefaultEndpointsProtocol=https;AccountName=dataaggregate001;AccountKey=/pnKVSJdy/Ri0N5LXgdKqRvfqp4Qw5ITubK4MdaGGlE+pruX5iQJBqjVhMECkzBO0hTV5Y47wRDgNxU5TU8W0A==;EndpointSuffix=core.windows.net"
        container_name = "test"

        ## Connection String
        connection_instance = azure_connect_conn_string(azure_connection_string, container_name)
        print('Done')
        return connection_instance

    except Exception as ex:
        print('main | Error: ', ex)


if __name__ == "__main__":
    container = main()
    container_content_list(container,"test/linnean_MMS/")

#
